package cn.gailvlun.gll.presentation.profile;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.github.markzhai.recyclerview.BaseViewAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.bean.PlaceHolder;
import cn.gailvlun.gll.databinding.ActivitySettingBinding;
import cn.gailvlun.gll.mgr.ShortcutMgr;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.base.WebViewActivity;
import cn.gailvlun.gll.presentation.profile.adapter.SettingAdapter;
import cn.gailvlun.gll.presentation.profile.bean.SettingViewModel;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;



public class SettingActivity extends BaseActivity {

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    private ActivitySettingBinding mBinding;

    private SettingAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_setting);
        mBinding.setPresenter(new Presenter());

        mAdapter = new SettingAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.recyclerView.setAdapter(mAdapter);

        mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        String[] strings = mContext.getResources().getStringArray(R.array.setting_title);
        List<SettingViewModel> settingViewModels = new ArrayList<>();
        String[] visibilities = mContext.getResources().getStringArray(R.array.setting_visibility);

        for (int i = 0; i < strings.length; i++) {
            SettingViewModel svm = new SettingViewModel(strings[i], i, visibilities[i]);
            if (svm.isVisible()) {
                settingViewModels.add(svm);
            }
        }

        mAdapter.set(settingViewModels);
    }

    public class Presenter implements BaseViewAdapter.Presenter {
        public void onSelected(SettingViewModel settingViewModel) {
            switch (settingViewModel.getOrder()) {
                case 0: {
                    AccountActivity.openActivity(mContext);
                    break;
                }
                case 1: {
                    Observable.just(1)
                            .doOnNext(new Consumer<Integer>() {
                                @Override
                                public void accept(Integer integer) throws Exception {
                                    Glide.get(mContext).clearDiskCache();
                                }
                            })
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .doOnNext(new Consumer<Integer>() {
                                @Override
                                public void accept(Integer integer) throws Exception {
                                    Glide.get(mContext).clearMemory();
                                    ToastUtil.showShort("缓存清理完毕");
                                }
                            })
                            .subscribe();
                    break;
                }
                case 4: {
                    WebViewActivity.openUserProtocolActivity(mContext);
                    break;
                }
                case 5: {
                    AboutActivity.openActivity(mContext);
                    break;
                }
            }
        }

        public void logOut() {
            new MaterialDialog.Builder(mContext)
                    .title("确定要退出当前账号吗？")
                    .negativeText("取消")
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                        }
                    })
                    .positiveText("确定")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                            realLogOut();
                        }
                    }).show();
        }
        public void realLogOut(){
            
            HttpMethods.getBaseService()
                    .logout()
                    .compose(RxUtil.<PlaceHolder>applyScheduler())
                    .subscribe(new HttpSubscriber<PlaceHolder>() {
                        @Override
                        protected void onSuccess(PlaceHolder response) {
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable PlaceHolder response, int code) {
                        }
                    });

            ShortcutMgr.logOut(mContext);
        }
    }
}
